import count from '@dfeidao/atom-nodejs/db/count';
import query from '@dfeidao/atom-nodejs/db/query';
// import { systemid } from 'src/atom/config';

interface Message {
	productid: string;
	page_size: number;
	page_no: number;
}

interface D {
	create_time1: string;
	create_time: number;
}

export default async function atom(msg: Message, action_id: string, session_id: string, spaceid: string) {
	const systemid = '12';
	const { exec, prepare } = query(action_id, session_id, systemid);
	prepare('auth_formgroup', ['_id', 'no', 'productid', 'name', 'create_time', 'priority'], { productid: msg.productid }, msg.page_size, msg.page_no, [], []);
	// 时间戳处理
	const [data] = await exec<D>();
	function format(date: number) {
		if (!date) {
			return '';
		}
		const _date = new Date(parseFloat(date + ''));
		let res = '';
		if (_date) {
			res = _date.getFullYear() + '-' + _format(_date.getMonth() + 1) + '-' + _format(_date.getDate());
		}
		return res;
	}

	function _format(d: number) {
		if (d < 10) {
			return '0' + d;
		} else {
			return d;
		}
	}
	// 格式化日期;
	data.forEach((item) => {
		item.create_time1 = item.create_time ? format(item.create_time) : '';
	});
	// 分页
	const c = count(action_id, session_id, systemid);
	c.prepare('auth_formgroup', { productid: msg.productid });
	const [t] = await c.exec();
	return { dataArray: data, count: t };
}
